package com.junxonline.instant.admin.service.config.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.junxonline.instant.dao.entity.dto.ui.SelectNodeDTO;
import com.junxonline.instant.dao.entity.model.config.Region;
import com.junxonline.instant.dao.entity.vo.config.RegionVO;
import com.junxonline.instant.dao.mapper.config.RegionMapper;
import com.junxonline.instant.admin.service.config.RegionService;
import com.junxonline.instant.common.entity.common.CommonReqPage;
import com.junxonline.instant.common.entity.common.CommonRespPage;
import com.junxonline.instant.common.enumeration.AdminErrorEnum;
import com.junxonline.instant.common.exception.AdminBizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 区域管理接口实现类
 *
 * @author JunX
 * @date 2021-07-18
 */
@Service
@Transactional
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService {

    @Autowired
    private RegionMapper regionMapper;

    /**
     * 查询区域列表（Select）
     *
     * @param wrapper 条件查询器
     * @return List<SelectNodeDTO>
     */
    @Override
    public List<SelectNodeDTO> querySelect(Wrapper<Region> wrapper) {
        List<SelectNodeDTO> regions = new ArrayList<>();
        List<Region> regionList = regionMapper.selectList(wrapper);
        for (Region region : regionList) {
            regions.add(new SelectNodeDTO(region.getId(), region.getId().toString(), region.getName()));
        }
        return regions;
    }

    /**
     * 查询区域列表
     *
     * @param vo 请求参数
     * @return CommonRespPage<Region>
     */
    @Override
    public CommonRespPage<Region> queryList(CommonReqPage<RegionVO> vo) {
        QueryWrapper<Region> wrapper = new QueryWrapper<>();
        wrapper.eq(vo.getQueryParams().getType() != null, "t.type", vo.getQueryParams().getType());
        wrapper.like(StrUtil.isNotBlank(vo.getQueryParams().getName()), "t.name", vo.getQueryParams().getName());
        wrapper.eq(vo.getQueryParams().getLevel() != null, "t.level", vo.getQueryParams().getLevel());
        wrapper.like(StrUtil.isNotBlank(vo.getQueryParams().getParentName()), "p.name", vo.getQueryParams().getParentName());
        return CommonRespPage.success(regionMapper.queryList(new Page<>(vo.getPage(), vo.getPageSize()), wrapper));
    }

    /**
     * 查询区域
     *
     * @param id ID
     * @return Region
     */
    @Override
    public Region query(Long id) {
        Region region = regionMapper.selectById(new QueryWrapper<Region>().eq("t.id", id));
        Assert.notNull(region, AdminErrorEnum.DATA_IS_NOT_EXISTS.getMsg());
        return region;
    }

    /**
     * 新增区域
     *
     * @param region 请求参数
     */
    @Override
    public void add(Region region) {
        Integer count = regionMapper.selectCount(new LambdaQueryWrapper<Region>()
                .eq(Region::getName, region.getName()));
        if (count > 0) {
            throw new AdminBizException(AdminErrorEnum.DATA_IS_EXISTS);
        }
        regionMapper.insert(region);
    }

    /**
     * 编辑区域
     *
     * @param region 请求参数
     */
    @Override
    public void edit(Region region) {
        Integer count = regionMapper.selectCount(new LambdaQueryWrapper<Region>()
                .ne(Region::getId, region.getId())
                .eq(Region::getName, region.getName()));
        if (count > 0) {
            throw new AdminBizException(AdminErrorEnum.DATA_IS_EXISTS);
        }
        regionMapper.updateById(region);
    }

    /**
     * 删除区域
     *
     * @param ids 请求参数
     */
    @Override
    public void delete(List<Long> ids) {
        regionMapper.deleteBatchIds(ids);
        for (Long regionId : ids) {
            regionMapper.delete(new LambdaUpdateWrapper<Region>().eq(Region::getParentId, regionId));
        }
    }

}
